from tkinter import Tk, Toplevel, Frame
from tkinter import Label, Button, Entry, LEFT, END

class LoginWindow(Tk):

    def __init__(self):
        super(LoginWindow, self).__init__()

        self.window_init()

        self.add_widgets()

        # self.on_login_button(lambda :print(self.get_username(), self.get_password()))
        # self.on_resetbutton_button(lambda: self.clear_username())

    def window_init(self):
        self.title('Login')
        self.resizable(False, False)

        window_w = 300
        window_h = 150

        pos_x = (self.winfo_screenwidth() - window_w) / 2
        pos_y = (self.winfo_screenheight() - window_h) / 2

        self.geometry("{}x{}+{}+{}".format(window_w, window_h, int(pos_x), int(pos_y)))

    def add_widgets(self):

        #用户名
        username_label = Label(self)
        username_label['text'] = '用户名:'
        username_label.grid(column=0, row=0, padx=5, pady=5)

        username_entry = Entry(self, name='username_entry')
        username_entry['width'] = 25
        username_entry.grid(column=1, row=0, padx=5, pady=5)


        #密码
        password_label = Label(self)
        password_label['text'] = '密码:'
        password_label.grid(column=0, row=1, padx=5, pady=5)

        password_entry = Entry(self, name='password_entry')
        password_entry['width'] = 25
        password_entry['show'] = '$'
        password_entry.grid(column=1, row=1, padx=5, pady=5)

        #按钮
        button_frame = Frame(self, name='button_frame')

        #重置按钮
        reset_button = Button(button_frame, name='reset_button')
        reset_button['text'] = '重置'
        reset_button.pack(side=LEFT, padx=25)

        #登录按钮
        login_button = Button(button_frame, name='login_button')
        login_button['text'] = '登录'
        login_button.pack(side=LEFT)

        button_frame.grid(row=2, columnspan=2, padx=5)

    def on_login_button(self, command):
        login_button = self.children['button_frame'].children['login_button']
        login_button['command'] = command

    def on_reset_button(self, command):
        reset_button = self.children['button_frame'].children['reset_button']
        reset_button['command'] = command

    def get_username(self):
        return self.children['username_entry'].get()

    def get_password(self):
        return self.children['password_entry'].get()

    def clear_username(self):
        self.children['username_entry'].delete(0, END)

    def clear_password(self):
        self.children['password_entry'].delete(0, END)

    def on_window_close(self, command):

        self.protocol('WM_DELETE_WINDOW', command)

if __name__ == '__main__':
    window = LoginWindow()
    window.mainloop()